class Template < ActiveRecord::Base

  paginates_per 15
  
  set_table_name :eshop_templates

  liquid_methods :current_user
  
  has_paper_trail
  diff :exclude => [:id, :created_at, :updated_at]
  
  validates :name, :presentation, :presence => true, :length => {:maximum => 30}
  validates :template, :presence => true
  validates :name, :uniqueness => true, :format => {:with => Format::STRING_REGEX}
  validates :group, :presence => true, :inclusion => {:in => ["system", "product_detail", "products_overview"]}
  validates_with LiquidTemplateFormat

  def self.get_template(name)
    @template = self.find_by_name(name)

    unless @template.blank?
      Liquid::Template.parse(@template.template)
    else
      raise Exceptions::TemplateNotFound.new("Šablona \"#{name}\" nebyla nalezena.")
    end
  end

  def current_user
    current_user
  end
end
